package web.directories.action;

import com.opensymphony.xwork2.ActionSupport;
import domain.entities.Cities;
import domain.entities.Regions;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import org.springframework.beans.factory.annotation.Autowired;
import services.AddressService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: nico
 * Date: 06/08/12
 * Time: 19:34
 * To change this template use File | Settings | File Templates.
 */
@ResultPath("/")
public class AddressController extends ActionSupport {

    private String countryId;
    private String regionId;
    private List<Regions> regions;
    private List<Cities> cities;
    private AddressService addressService;
    private String phone;

    @Autowired
    public AddressController(AddressService addressService) {
        this.addressService = addressService;
        this.regions = new ArrayList<Regions>();
        this.cities = new ArrayList<Cities>();
    }

    @Action(value="getRegions", results={
            @Result(name="success",location="WEB-INF/jsp/Directories/address/regions.jsp")
    })
    public String execute() {
        Short countryId = Short.parseShort(this.countryId);
        regions = addressService.getRegions(countryId);
        return SUCCESS;
    }

    @Action(value="getCountryCode", results={
            @Result(name="success",location="WEB-INF/jsp/Directories/address/phone.jsp")
    })
    public String getCountryCode() {
        Short countryId = Short.parseShort(this.countryId);
        phone = addressService.getCountryCode(countryId);
        return SUCCESS;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Action(value="getRegionsCenter", results={
            @Result(name="success",location="WEB-INF/jsp/Directories/address/regionsCenter.jsp")
    })
    public String regionsCenter() {
        Short countryId = Short.parseShort(this.countryId);
        regions = addressService.getRegions(countryId);
        return SUCCESS;
    }

    @Action(value="getCities", results={
            @Result(name="success",location="WEB-INF/jsp/Directories/address/cities.jsp")
    })
    public String cities() {
        Short regionId = Short.parseShort(this.regionId);
        cities = addressService.getCities(regionId);
        Cities c = new Cities(countryId, regionId.toString(), "Other", new Float(0), new Float(0), "");
        c.setCityId(-2);
        cities.add(c);

        return SUCCESS;
    }

    @Action(value="getCitiesCenter", results={
            @Result(name="success",location="WEB-INF/jsp/Directories/address/citiesCenter.jsp")
    })
    public String citiesCenter() {
        Short regionId = Short.parseShort(this.regionId);
        cities = addressService.getCities(regionId);
        Cities c = new Cities(countryId, regionId.toString(), "Other", new Float(0), new Float(0), "");
        c.setCityId(-2);
        cities.add(c);

        return SUCCESS;
    }

    public String getCountryId() {
        return countryId;
    }

    public void setCountryId(String countryId) {
        this.countryId = countryId;
    }

    public List<Regions> getRegions() {
        return regions;
    }

    public void setRegions(List<Regions> regions) {
        this.regions = regions;
    }

    public String getRegionId() {
        return regionId;
    }

    public void setRegionId(String regionId) {
        this.regionId = regionId;
    }

    public List<Cities> getCities() {
        return cities;
    }

    public void setCities(List<Cities> cities) {
        this.cities = cities;
    }
}
